package uns.fit.gis.ui;

import javax.microedition.lcdui.ChoiceGroup;
import javax.microedition.lcdui.Command;
import javax.microedition.lcdui.CommandListener;
import javax.microedition.lcdui.Displayable;
import javax.microedition.lcdui.Form;
import javax.microedition.lcdui.Item;
import javax.microedition.lcdui.ItemStateListener;
import javax.microedition.lcdui.TextField;

import uns.fit.gis.MapDrawer;
import uns.fit.gis.Util;
import uns.fit.gis.VietnameseConverter;
import uns.fit.gis.i18n.LocaleManager;
import uns.fit.gis.mapdrawer.MapCanvas;
import uns.fit.gis.mapobj.Segment;

public class SearchPathForm extends Form implements ItemStateListener,
		CommandListener {
	private int choiceIndex = 0;
	private int baseIndex = 0;
	private MapDrawer midlet;
    private String strQuery;
    private String pathName;
    private int nPaths;
        
    public static boolean showLocationsAlongPath;
        
    private Command backCommand = new Command(LocaleManager.getMessage("back"), Command.BACK, 0);
    private Command selectCommand = new Command(LocaleManager.getMessage("showmap"), Command.ITEM, 1);
    private Command prevCommand = new Command(LocaleManager.getMessage("next"), Command.ITEM, 1);
    private Command nextCommand = new Command(LocaleManager.getMessage("prev"), Command.ITEM, 1);
    private TextField textField = new TextField(LocaleManager.getMessage("name"), "", 20,
    			TextField.ANY);
    private ChoiceGroup choiceGroup = new ChoiceGroup(LocaleManager.getMessage("result"), ChoiceGroup.EXCLUSIVE);
    	
    private int[] choiceGroupPathId;
    public static int selectedPathId = -1;
    
	public SearchPathForm(String title, MapDrawer midlet) {
		super(title);
		showLocationsAlongPath = false;
		textField.setInitialInputMode("MIDP_UPPERCASE_LATIN");
		append(textField);
		append(choiceGroup);
		addCommand(backCommand);
		addCommand(selectCommand);
		addCommand(prevCommand);
		addCommand(nextCommand);
		this.midlet = midlet;
		setItemStateListener(this);
		setCommandListener(this);
		choiceGroupPathId = new int[10];
                nPaths = MapCanvas.paths.length;
		for(int i = 0; i < choiceGroupPathId.length; i ++)
			choiceGroupPathId[i] = -1;
	}

	public SearchPathForm(String arg0, Item[] arg1) {
		super(arg0, arg1);
		// TODO Auto-generated constructor stub
	}

	public void itemStateChanged(Item item) {
		if (item == textField) {
			baseIndex = 0;
			updateChoiceGroup();
		}
		//else if(item == choiceGroup) {
		//	selectedPathId = choiceGroupPathId[choiceGroup.getSelectedIndex()];
		//}
	}
	
	private void updateChoiceGroup() {
		if (textField.getString().length() > 0) {
			//textField.setString(textField.getString().toUpperCase());
			choiceGroup.deleteAll();
			choiceIndex = 0;
                            strQuery = VietnameseConverter.toTextNotMarked(textField.getString()).toUpperCase();
                            //System.out.println(str);
			for (int i = 0; i < nPaths; i++) {
				pathName = Util.decodeCharacters(MapCanvas.paths[i].getName());
				//System.out.println(str + " " + textField.getString());
				if (VietnameseConverter.toTextNotMarked(pathName).toUpperCase().indexOf(strQuery) != -1) {
					if(choiceIndex >= baseIndex && choiceIndex < baseIndex + 10) {
						choiceGroup.append(pathName, null);
						choiceGroupPathId[choiceIndex - baseIndex] = i;
					}
					choiceIndex ++;
				}
			}
		}
	}

	public void commandAction(Command c, Displayable d) {
		if (c == backCommand) {
			midlet.showMapCanvas();
		}
		else if(c == selectCommand) {
			if(choiceGroup.getSelectedIndex() != -1) {
				selectedPathId = choiceGroupPathId[choiceGroup.getSelectedIndex()];
				if(selectedPathId != -1) {
					Segment segment = MapCanvas.segments[MapCanvas.paths[SearchPathForm.selectedPathId].getSegments()[0]];
					Util.centerScreen(segment.getX(1), segment.getY(1));
				}
				midlet.showMapCanvas();
			}
		}
		else if(c == prevCommand) {
			if(baseIndex > 9) {
				baseIndex -= 10;
				updateChoiceGroup();
			}
		}
		else if(c == nextCommand) {
			if(choiceGroup.size() > 0) {
				baseIndex += 10;
				updateChoiceGroup();
			}
		}
	}

	protected void keyPressed(int keyCode) {
		
	}
}
